/*
 * Created on 2004-5-29 TODO To change the template for this generated file go
 * to Window - Preferences - Java - Code Generation - Code and Comments
 */

package cn.com.yinhex.as.util;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author test TODO To change the template for this generated type comment go
 *         to Window - Preferences - Java - Code Generation - Code and Comments
 */
public class DOCUtil {
	/**
	 * }��DOC�������
	 * 
	 * @param docA
	 * @param docB
	 * @param key
	 * @return
	 */
	public static DOC[] sub(DOC[] docA, DOC[] docB, String key) {
		try {
			if (!isSame(docA[0], docB[0])) {
				return null;
			}
			return null;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * }��DOC�������
	 * 
	 * @param docA
	 * @param docB
	 * @return
	 */
	public static DOC[] add(DOC[] docA, DOC[] docB) {
		try {
			if (docB == null) {
				return docA;
			}
			if (docA == null) {
				return docB;
			}
			if (!isSame(docA[0], docB[0])) {
				return docA;
			}

			DOC[] docs = new DOC[docA.length + docB.length];
			for (int i = 0; i < docA.length; i++) {
				docs[i] = docA[i];
			}
			for (int i = 0; i < docB.length; i++) {
				docs[i + docA.length] = docB[i];
			}
			return docs;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static DOC[] addone(DOC[] a, DOC b) {
		if (b == null) {
			return a;
		}
		if (a == null) {
			DOC[] doc = new DOC[1];
			doc[0] = b;
			return doc;
		}
		DOC[] doc = new DOC[a.length + 1];
		for (int i = 0; i < a.length; i++) {
			doc[i] = a[i];
		}
		doc[a.length] = b;
		return doc;
	}

	/**
	 * DOC�����>�key����
	 * 
	 * @param doc
	 * @param key
	 * @param desc
	 * @return
	 */
	public static DOC[] sort(DOC[] docs, String key, boolean desc) {
		if (docs == null) {
			return null;
		}
		KeyComparator keycomp = new KeyComparator();
		keycomp.setKeys(new String[] { key }, new int[] { KeyComparator.STR_KEY }, desc);
		Arrays.sort(docs, keycomp);
		return docs;
	}

	/**
	 * DOC����ȡǰ����
	 */
	public static DOC[] cut(DOC[] docs, int num) {
		return cut(docs, 0, num);
	}

	/**
	 * DOC����ȡָ�����
	 */
	public static DOC[] cut(DOC[] docs, int start, int num) {
		if (docs == null) {
			return null;
		}
		if (docs.length < start) {
			return null;
		}
		if (docs.length <= start + num) {
			return cut(docs, start, docs.length - start);
		}
		DOC[] tmpdocs = new DOC[num];
		for (int i = start; i < start + num; i++) {
			tmpdocs[i - start] = docs[i];
		}
		return tmpdocs;
	}

	/**
	 * DOC�����>�һ��key����
	 * 
	 * @param doc
	 * @param key
	 * @param desc
	 * @return
	 */
	public static DOC[] multisort(DOC[] docs, String[] keys, int[] keytypes, boolean desc) {
		KeyComparator keycomp = new KeyComparator();
		keycomp.setKeys(keys, keytypes, desc);
		Arrays.sort(docs, keycomp);
		return docs;
	}

	/**
	 * }��DOC�Ľṹ�Ƿ���ͬ
	 * 
	 * @param a
	 * @param b
	 * @return
	 */
	public static boolean isSame(DOC a, DOC b) {
		try {
			// �õ�doc������key��
			String[] keysa = a.getKeys();
			String[] keysb = b.getKeys();
			// key����ͬ���϶���}��doc�Ľṹ��ͬ
			if (keysa.length != keysb.length) {
				return false;
			}
			// ������
			Arrays.sort(keysa);
			Arrays.sort(keysb);
			// ѭ���ж�ÿ��key�Ƿ���ͬ��ȫ��ͬ�򷵻�true
			for (int i = 0; i < keysa.length; i++) {
				if (!keysa[i].equals(keysb[i])) {
					return false;
				}
			}
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	public static void main(String[] args) {
		DOC[] doc = FileUtil.listTheFolder(".");
		doc = sort(doc, "filesize", true);
		for (int i = 0; i < doc.length; i++) {
			System.out.print(doc[i].get("modtime") + "-----");
			System.out.print(StringUtil.addzero(Integer.parseInt(doc[i].get("filesize")), 10) + "-----");
			System.out.print(StringUtil.addzero(Integer.parseInt(doc[i].get("hashcode")), 12) + "-----");
			System.out.println(doc[i].get("filename"));
		}
	}
}

class KeyComparator implements Comparator {
	private String[] keys = null;
	private int[] keytypes = null;
	private boolean desc = true;

	public static final int INT_KEY = 1;
	public static final int STR_KEY = 2;

	public void setKeys(String[] keys, int[] keytypes, boolean desc) {
		this.keys = keys;
		this.keytypes = keytypes;
		this.desc = desc;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object o1, Object o2) {
		// TODO Auto-generated method stub
		DOC lit1 = (DOC) o1;
		DOC lit2 = (DOC) o2;
		for (int i = 0; i < keys.length; i++) {
			if (keytypes[i] == INT_KEY) {
				if (lit1.geti(keys[i]) < lit2.geti(keys[i])) {
					return desc ? 1 : -1;
				} else if (lit1.geti(keys[i]) > lit2.geti(keys[i])) {
					return desc ? -1 : 1;
				}
			} else if (keytypes[i] == STR_KEY) {
				String value1 = lit1.get(keys[i]);
				String value2 = lit2.get(keys[i]);
				char[] cs1 = value1.toCharArray();
				char[] cs2 = value2.toCharArray();
				int minlength = (cs1.length > cs2.length) ? cs2.length : cs1.length;
				for (int j = 0; j < minlength; j++) {
					if (cs1[j] < cs2[j]) {
						return desc ? 1 : -1;
					} else if (cs1[j] > cs2[j]) {
						return desc ? -1 : 1;
					}
				}
			}
		}
		return 0;
	}
}